package com.mylg.craw.house.government.category;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
@Setter
@ToString
public class SellStatistics {
    private static final List<Pattern> PRICE = Arrays.asList(
            Pattern.compile("均价(\\d+(\\.\\d+)?)[\\u4e00-\\u9fa5]*"),
            Pattern.compile("均价为(\\d+(\\.\\d+)?)[\\u4e00-\\u9fa5]*"),
            Pattern.compile("价格为(\\d+(\\.\\d+)?)[\\u4e00-\\u9fa5]*")
    );
    private static final List<Pattern> SELL = Arrays.asList(
            Pattern.compile("销售(\\d+(\\.\\d+)?)[\\u4e00-\\u9fa5]*"),
            Pattern.compile("成交(\\d+(\\.\\d+)?)[\\u4e00-\\u9fa5]*")
    );
    private static final Pattern SELL_AREA = Pattern.compile("面积(\\d+(\\.\\d+)?)[\\u4e00-\\u9fa5]*");
    //总数
    private String number;

    private String area;

    //住宅
    private String avgPrice;

    private int endIndex;

    public static SellStatistics parse(String text) {
        SellStatistics sellStatistics = new SellStatistics();
        Matcher matcher;
        for (Pattern pattern : PRICE) {
            matcher = pattern.matcher(text);
            if (matcher.find()) {
                String content = matcher.group(0);
                sellStatistics.endIndex = Math.max(sellStatistics.endIndex, text.indexOf(content) + content.length());
                sellStatistics.setAvgPrice(matcher.group(1));
                break;
            }
        }

        for (Pattern pattern : SELL) {
            matcher = pattern.matcher(text);
            if (matcher.find()) {
                String content = matcher.group(0);
                sellStatistics.endIndex = Math.max(sellStatistics.endIndex, text.indexOf(content) + content.length());
                sellStatistics.setNumber(matcher.group(1));
                break;
            }
        }

        matcher = SELL_AREA.matcher(text);
        if (matcher.find()) {
            String content = matcher.group(0);
            sellStatistics.endIndex = Math.max(sellStatistics.endIndex, text.indexOf(content) + content.length());
            sellStatistics.setArea(matcher.group(1));
        }

        return sellStatistics;
    }

    public static SellStatistics parse1(String text) {
        String[] segments = text.split("，");
        Assert.isTrue(segments.length >= 3, "实际销售统计的某一项为空:" + text);
        SellStatistics sellStatistics = new SellStatistics();
        for (String sege : segments) {
            int index = sege.indexOf("商品房销售");
            if (index > 0) {
                sellStatistics.setNumber(sege.substring(index));
                continue;
            }

            index = sege.indexOf("销售面积");
            if (index > 0) {
                sellStatistics.setArea(sege.substring(index));
                continue;
            }

            index = sege.indexOf("销售均价");
            if (index > 0) {
                sellStatistics.setAvgPrice(sege.substring(index));
                continue;
            }

        }
        return sellStatistics;
    }
}
